//
//  LXTYQBirthdayHelper.swift
//  LuoXuanTYQ
//
//  Created by 李东 on 2024/3/17.
//

import UIKit
import HandyJSON

class LXTYQBirthdayHelper: NSObject {
    
    static let shared = LXTYQBirthdayHelper()
    
    let LocalKey = "birthdayKey"
    lazy var array = [LXTYQBirthdayModel]()

    
    func getLocalList() -> [LXTYQBirthdayModel] {
        if !array.isEmpty {
            return array
        }
        if let tt = UserDefaults.standard.object(forKey: LocalKey) as? String {
            if let list = [LXTYQBirthdayModel].deserialize(from: tt) as? [LXTYQBirthdayModel] {
                array = list
            }
        }
        return array
    }
    
    func addDate(model: LXTYQBirthdayModel)  {
        if array.contains(where: {$0.ids == model.ids}) {
            if let ii = array.firstIndex(where: {$0.ids == model.ids}) {
                array[ii] = model
            }
        }else {
            array.append(model)
        }
        if let json = array.toJSONString() {
            UserDefaults.standard.set(json, forKey: LocalKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    func deleteDate(ids: Int)  {
        array.removeAll(where: {$0.ids == ids})
        if let json = array.toJSONString() {
            UserDefaults.standard.set(json, forKey: LocalKey)
            UserDefaults.standard.synchronize()
        }
    }
}
